//=============================================================================
// System  : MatchKit
// File    : EnumPattern.cs
// Author  : Fabio Bussu  (matchkit@fabiobussu.it)
// Updated : 04/16/2013
// Note    : Copyright 2013, Fabio Bussu, All rights reserved
//
// This code is published under the Microsoft Public License (Ms-PL). A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://matchkit.codeplex.com. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// Version     Date     Who  Comments
// ============================================================================
// 1.0.0.0  04/16/2013   FB  First draft
//=============================================================================
using System;
using System.Runtime.Serialization;

namespace MatchKit.Patterns
{
	/// <summary>
	/// Provides the logic to match a string that is a name of an enum's element.
	/// </summary>
	/// <threadsafety static="true" instance="false" />
	/// <serialization binary="true" contract="true" />
	/// <conceptualLink target="cb735773-b793-4cc4-acc4-6688cdb226b6" />
	/// <conceptualLink target="0a06b2a9-5f58-409a-ba62-639cfd77bca7" />
	#if DC
	[DataContract]
	#endif
	#if SZ
	[Serializable]
	#endif
	public sealed class EnumPattern : BasePattern
	{
		#if SZ
		[NonSerialized]
		#endif
		private string[] _names = null;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.Patterns.EnumPattern"/> class. 
		/// </summary>
		/// <param name="enumType">Type of the enum to match. See <see cref="P:MatchKit.Patterns.EnumPattern.EnumType"/>.</param>
		public EnumPattern(Type enumType)
			: this(enumType, true)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.Patterns.EnumPattern"/> class. 
		/// </summary>
		/// <param name="enumType">Type of the enum to match. See <see cref="P:MatchKit.Patterns.EnumPattern.EnumType"/>.</param>
		/// <param name="ignoreCase">Specifies whether to perform a case insensitive comparison. See <see cref="P:EnumPattern.IgnoreCase"/>.</param>
		public EnumPattern(Type enumType, bool ignoreCase)
		{
			EnumType = enumType;
			IgnoreCase = ignoreCase;
		}

		private Type _enumType;
		/// <summary>
		/// Gets or sets the type of the enum to match.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public Type EnumType
		{
			get { return _enumType; }
			set
			{
				if (value == null)
					throw new ArgumentNullException("value", "EnumType cannot be null");
				if (!value.IsEnum)
					throw new ArgumentException("EnumType must be an enum", "value");
				_enumType = value;
			}
		}

		private bool _ignoreCase = true;
		/// <summary>
		/// Gets or sets the case sensitive option. Default is <b>true</b>.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public bool IgnoreCase
		{
			get { return _ignoreCase; }
			set { _ignoreCase = value; }
		}

		/// <inheritdoc />
		protected override Match OnMatch(MatchContext context)
		{
			if (_names == null)
				_names = Utils.GetNames(_enumType);

			foreach (var name in _names)
			{
				var branch = context.Branch();
				if (branch.NextWith(name, _ignoreCase))
				{
					// e' richiesta la parola esatta, se il carattere successivo e' ancora una lettera, passo al prox enum item
					if (branch.IsLetter)
						continue;
					context.Commit(branch);
					return Success(context, Enum.Parse(_enumType, name, _ignoreCase));
				}
			}

			return Error(context);
		}

		/// <inheritdoc />
		public override string HelpString
		{
			get { return "Element of enum <" + _enumType.Name + ">"; }
		}
	}
}
